package com.micro.ai.models.config;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.method.configuration.EnableMethodSecurity;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.http.SessionCreationPolicy;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.security.web.SecurityFilterChain;
import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter;

/**
 * Spring Security配置
 * 
 * @author micro-ai
 * @since 0.0.1
 */
@Configuration
@EnableWebSecurity
@EnableMethodSecurity
public class SecurityConfig {

    @Autowired
    private GatewayAuthFilter gatewayAuthFilter;

    /**
     * 密码编码器
     * 使用BCrypt算法，强度为12
     * 
     * @return PasswordEncoder
     */
    @Bean
    public PasswordEncoder passwordEncoder() {
        return new BCryptPasswordEncoder(12);
    }

    /**
     * 安全过滤器链
     * 
     * @param http HttpSecurity
     * @return SecurityFilterChain
     */
    @Bean
    public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
        http
                // 禁用CSRF（因为使用JWT）
                .csrf(csrf -> csrf.disable())
                
                // 禁用CORS
                .cors(cors -> cors.disable())
                
                // 会话管理：无状态
                .sessionManagement(session -> 
                    session.sessionCreationPolicy(SessionCreationPolicy.STATELESS)
                )
                
                // 添加网关认证过滤器（在 UsernamePasswordAuthenticationFilter 之前）
                .addFilterBefore(gatewayAuthFilter, UsernamePasswordAuthenticationFilter.class)
                
                // 授权规则
                .authorizeHttpRequests(auth -> auth
                    // 公开接口
                    .requestMatchers(
                        "/actuator/**",
                        "/error",
                        // API文档相关路径
                        "/doc.html",
                        "/swagger-ui.html",
                        "/swagger-ui/**",
                        "/v3/api-docs/**",
                        "/swagger-resources/**",
                        "/webjars/**",
                        "/favicon.ico"
                    ).permitAll()
                    .anyRequest().authenticated()
                )
                
                // 禁用默认登录页面
                .formLogin(form -> form.disable())
                
                // 禁用默认登出
                .logout(logout -> logout.disable());

        return http.build();
    }
}

